package GUI;

import java.util.ArrayList;


public class Cell{

	private ArrayList<CellChangedListener> celllistener = new ArrayList<CellChangedListener>(); 
	
	
	private boolean left, top, right, bottom;
	private int posx,posy;
	
	public static final int TOP = 0;
	public static final int BOTTOM = 1;
	public static final int RIGHT = 2;
	public static final int LEFT = 3;
	
	private int group;
	private boolean groupChanged = false;
	
	public Cell(int posx,int posy) {
		left = top = right = bottom = true;
		this.posx = posx;
		this.posy = posy;
	}

	
	public void setWalls(boolean l, boolean t, boolean r, boolean b) {
		left = l;
		top = t;
		right = r;
		bottom = b;
	}
	
	public void setBackground(int color){
		setBackground(color);
	}

	public boolean isLeft() {
		return left;
	}

	public void setLeft(boolean left) {
		this.left = left;
		callCellChangedListener();
	}

	public boolean isTop() {
		return top;
	}

	public void setTop(boolean top) {
		this.top = top;
		callCellChangedListener();
	}

	public boolean isRight() {
		return right;
	}

	public void setRight(boolean right) {
		this.right = right;		
		callCellChangedListener();
	}

	public boolean isBottom() {
		return bottom;
	}

	public void setBottom(boolean bottom) {
		this.bottom = bottom;
		callCellChangedListener();
	}
	
	public int getPosx() {
		return posx;
	}

	public int getPosy() {
		return posy;
	}
	
	public boolean wallsIntact(){
		return (top && bottom && right && left) ? true : false;
	}

	public void addCellChangedListener(CellChangedListener ccl){
		celllistener.add(ccl);
	}
	
	public void removeCellChangedListener(CellChangedListener ccl){
		celllistener.remove(ccl);
	}
	
	private void callCellChangedListener(){
		for(CellChangedListener c : celllistener)
			c.cellChanged();
	}
	
	
	
	public int position(Cell other){
		int ox = other.getPosx();
		int oy = other.getPosy();
		
		if(ox < posx && oy == posy)
			return LEFT;
		if(ox > posx && oy == posy)
			return RIGHT;
		if(ox == posx && oy < posy)
			return TOP;
		if(ox == posx && oy > posy)
			return BOTTOM;
		
		return -1;
	}
	
	@Override
	public String toString() {
		return "XPos: " + posx + " YPos: " + posy;
	}
	
	@Override
	public boolean equals(Object obj) {
		Cell c = (Cell)obj;
		return (c.getPosx() == getPosx() && c.getPosy() == getPosy());
	}
	
	public void setGroup(int g){
		group = g;
		groupChanged = true;
	}
	
	public boolean groupChanged(){
		return groupChanged;
	}

	public void initialGroupSet(int g){
		group = g;
	}
	
	public int getGroup(){
		return group;
	}

}
